// created Jan 9, 2009 10:57:52 AM
// by petrovic
/*
Copyright 2009 Homosuperiorus LLC 

Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

*/

package org.taglets.pub.tags;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.logging.Logger;

import org.taglets.exception.AuthenticationFailedException;
import org.taglets.exception.RateLimitExceededException;
import org.taglets.exception.ServerErrorException;
import org.taglets.exception.TagNotFoundException;
import org.taglets.pub.AppResponse;
import org.taglets.pub.HttpResponse;

public final class TagStatusResponse extends AppResponse {
    @SuppressWarnings("unused")
    private static Logger LOG = Logger.getLogger(TagStatusResponse.class.getName());

    private Tag tag;

    public TagStatusResponse(HttpResponse httpResponse) throws IOException, ServerErrorException, TagNotFoundException, AuthenticationFailedException, RateLimitExceededException {
        super(httpResponse);
        if (responseCode == HttpURLConnection.HTTP_OK) {
            succeeded = true;
            tag = Tag.buildFollowedTag(getInputStream());
        } else if (responseCode == HttpURLConnection.HTTP_PRECON_FAILED) {
            throw new AuthenticationFailedException();
        } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
            throw new TagNotFoundException();
        } else if (responseCode == HttpURLConnection.HTTP_GONE) {
            throw new RateLimitExceededException();
        } else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
            throw new ServerErrorException();
        }
    }

    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }
}

/*
 * Typical input to XML parser:
<?xml version="1.0" encoding="UTF-8"?>
<tag created="2009-01-10 03:23:27.767877" description="this is a British Airways flight BA209 descriptor-0" following="true" name="tag-0" ncomment="1">
  <outlets>
    <outlet protocol="email" uri="mark@carpediemcg.com"/>
    <outlet protocol="http" uri="http://www.petrovic.org/taglet/comment"/>
    <outlet protocol="twitter" uri="wn0owp"/>
  </outlets>
</tag>
 */

